/**
 * @Author 韦建韩
 * @Date 2022/5/20
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.Manage.UserManager.UserInfoManage;
import com.example.devops.dao.UserMapper;
import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.User;
import com.example.devops.exception.ValidationException;
import com.example.devops.service.impl.InstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("instance")
public class InstanceController {
    @Autowired
    InstanceService instanceService;
    @Autowired
    UserMapper userMapper;
    @RequestMapping("stopInstance")
    public Result stopInstance(Long id) {
        User user = UserInfoManage.get();
        User user1 = userMapper.selectById(user.getId());
        if (user1.getPrivilege() != null && user1.getPrivilege() != null) {
            if (user1.getPrivilege().contains("3")) {
                instanceService.stopInstance(id);
            } else {
                throw new ValidationException("该人员没有创建镜像的权限");
            }
        }
        return Result.doCreate();
    }

    @RequestMapping("deleteInstance")
    public Result deleteInstance(Long id) {
        User user = UserInfoManage.get();
        User user1 = userMapper.selectById(user.getId());
        if (user1.getPrivilege() != null && user1.getPrivilege() != null) {
            if (user1.getPrivilege().contains("3")) {
                instanceService.deleteInstance(id);
            } else {
                throw new ValidationException("该人员没有创建镜像的权限");
            }
        } else {
            throw new ValidationException("该人员没有创建镜像的权限");
        }
        return Result.doCreate();
    }

}
